﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using HtmlAgilityPack;
using DBService;
using DBService.ViewDatas;

public partial class administration_ParseYellGE : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {   
        if (!IsPostBack)
        {
            var ma = new ModelAdapter();
            var caters = ma.GetCategoreis(null);
            ddlCats.DataSource = caters;
            ddlCats.DataTextField = "Title";
            ddlCats.DataValueField = "CategoryID";
            ddlCats.DataBind();
        }
    }

    protected void ParseButton_Click(object sender, EventArgs e)
    {
        try
        {
            using (var wc = new WebClient())
            {
                var htmlData = wc.DownloadString(txtUrl.Text);
                wc.Encoding = System.Text.Encoding.UTF8;
                var doc = new HtmlDocument();
                doc.LoadHtml(htmlData);


                var ma = new ModelAdapter();
                var entities = doc.DocumentNode.SelectNodes("//div[@class='y_result_w']");
                var users = ma.GetTopMembersForRatings2();
                users.Shuffle();
                for (int i = 0; i < entities.Count; i++)
                {
                    var u = users[0];
                    try
                    {
                        entities[i].SelectNodes("//div[@class='y_title']");
                        var title = entities[i].SelectNodes("//div[@class='y_title']")[i].InnerText;
                        var contact = entities[i].SelectNodes("//div[@class='y_sub']")[i].InnerText;
                        var address = contact.Substring(0, contact.IndexOf("ტელ") - 3);
                        var web = entities[i].SelectNodes("//a[@class='y_link']")[i].Attributes["href"].Value;

                        int lentel = contact.IndexOf("ტელ") + 4;
                        int len = 0;
                        if (contact.Contains("ფაქსი"))
                        {
                            len = contact.IndexOf("ფაქსი") - lentel;
                        }
                        else
                        {
                            len = contact.Length - lentel;
                        }
                        var phone = contact.Substring(contact.IndexOf("ტელ") + 5, len - 1);
                        var phhhhhhhhhhhhhh = phone.Split(',').ToList();
                        var phones = new List<string>();
                        phones.AddRange(
                            phone
                            .Split(',').ToList()
                            .Select(s => s.Trim(',', ' '))
                            .Where(s => !String.IsNullOrEmpty(s))
                            .ToList()
                        );


                        var acid = ma.CompanyActionShort(null, null, title, "", String.Empty, web,
                            new List<int>() { int.Parse(ddlCats.SelectedValue) },
                                  new List<string>(), u.UserId);


                        if (!string.IsNullOrEmpty(address))
                        {
                            List<CompanyOption> compOptions = new List<CompanyOption>();
                            int pa = 0;
                            foreach (string entity in phones)
                            {
                                pa++;
                                var adr = "";
                                if (!String.IsNullOrEmpty(address) && pa==1)
                                {
                                    adr = address;
                                }
                                compOptions.Add(new CompanyOption()
                                {
                                    CompanyID = acid,
                                    ModifierUserID = u.UserId,
                                    OptionName = adr,
                                    OptionTypeID = (int)CompanyOptionType.ADDRESS_PHONE,
                                    SortIndex = null,
                                    Value = entity
                                });
                            }
                            ma.AddCompanyOptions(compOptions, acid, u.UserId);
                        }
                    }
                    catch { }
                }
            }
        }
        catch (Exception ex)
        {

        }
    }


}
static class MyClass
{
    public static void Shuffle<T>(this IList<T> list)
    {
        Random rng = new Random();
        int n = list.Count;
        while (n > 1)
        {
            n--;
            int k = rng.Next(n + 1);
            T value = list[k];
            list[k] = list[n];
            list[n] = value;
        }
    }
}